﻿/*******************************************************************
 *     JameE
 *******************************************************************
  Copyright 2010 Clifford Champion and JameE contributors.
  
  This file is part of JameE.
  
  JameE is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  JameE is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with JameE.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************/


$class_static(
"JameE.Utils.Packing",
{
	/////////////////////
	// FIELDS
	/////////////


	/////////////////////
	// CONSTRUCTORS
	/////////////
	_ctor: function () {
		this._hash_oTimersByName = {};
		this._hash_oDurationsByName = {};
	},


	/////////////////////
	// METHODS
	/////////////
	packFloatToInteger: function (fValue, iIntegerMin, iIntegerMax, fFloatMin, fFloatMax) {
		$require_type(fValue, "number");
		$require_type(iIntegerMin, "number");
		$require_type(iIntegerMax, "number");
		$require_type(fFloatMin, "number");
		$require_type(fFloatMax, "number");

		if (fValue >= fFloatMax) {
			return parseInt(iIntegerMax);
		}
		if (fValue <= fFloatMin) {
			return parseInt(iIntegerMin);
		}

		var fValueRemapped = ((fValue - fFloatMin) / (fFloatMax - fFloatMin)) * (iIntegerMax - iIntegerMin) + iIntegerMin;
		fValueRemapped = Math.round(fValueRemapped);
		fValueRemapped = Math.max(iIntegerMin, fValueRemapped);
		fValueRemapped = Math.min(iIntegerMax, fValueRemapped);
		return parseInt(fValueRemapped);
	}
});

$include_notify("Utils/Packing.js");

